<script setup lang="ts">
import {
  getDeviceListAPI,
  getDeviceDetailSerialAPI,
  deleteDeviceAPI
} from "@/api/destribute.ts";
import { onMounted, ref } from "vue";
import { deviceItem } from "@/type/destribute.ts";
import { ElMessage } from "element-plus";
import { Delete, Edit, InfoFilled } from "@element-plus/icons-vue";
import FormDialog from "@/view/Distribute/Equip/components/form-dialog.vue";
import DetailDialog from "@/view/Distribute/Equip/components/detail-dialog.vue";

// 设备列表
const deviceList = ref<deviceItem[]>()
const formDialog = ref()
const detailDialog = ref()
const getDeviceData = async () => {
  const res = await getDeviceListAPI()
  deviceList.value = res.data
}

// 搜索设备
const serialNumber = ref('')
const deviceDetail = ref<deviceItem>()
const searchDevice = async () => {
  const res = await getDeviceDetailSerialAPI(serialNumber.value)
  deviceDetail.value = res.data
  detailDialog.value.init(deviceDetail.value)
  serialNumber.value = ''
}

// 添加设备
const addDevice = () => {
  formDialog.value.init({}, 0)
}

// 编辑设备
const editDevice = (row: any) => {
  formDialog.value.init(row, 1)
}

// 删除设备
const deleteDevice = async (id: string) => {
  const res: any = await deleteDeviceAPI(id)
  await getDeviceData()
  ElMessage({
    type: 'success',
    message: res.message
  })
}

onMounted(() => {
  getDeviceData()
})
</script>

<template>
  <el-card class="box-card">
    <div class="card-equip-title">
      <div class="row align_center">
        <el-input v-model="serialNumber" clearable placeholder="请输入型号"/>
        <el-button style="margin-left: 20px" type="primary" @click="searchDevice">查看设备详情</el-button>
      </div>
      <el-button @click="addDevice">添加设备</el-button>
    </div>
    <div class="card-equip-table">
      <el-table
        border
        class="el-table-box"
        ref="multipleTableRef"
        :data="deviceList"
        style="width: 100%"
      >
        <el-table-column label="#" width="60" align="center" type="index"/>
        <el-table-column prop="name" align="center" label="名称"/>
        <el-table-column prop="purchaseDate" align="center" label="购买日期"/>
        <el-table-column prop="purchasePrice" align="center" label="购买金额" width="100"/>
        <el-table-column prop="serialNumber" align="center" label="型号"/>
        <el-table-column prop="number" align="center" label="数量"/>
        <el-table-column prop="status" align="center" label="状态" width="100"/>
        <el-table-column align="center" label="控制" width="180">
          <template #default="{row}">
            <div class="operator-btn">
              <el-button type="primary" size="default" :icon="Edit as any" @click="editDevice(row)" circle/>
              <el-popconfirm
                width="200"
                @confirm="deleteDevice(row._id)"
                confirm-button-text="确定"
                cancel-button-text="取消"
                :icon="InfoFilled as any"
                icon-color="#f36b6c"
                title="您确定删除此设备吗?"
              >
                <template #reference>
                  <el-button type="danger" size="default" :icon="Delete as any" circle/>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <FormDialog ref="formDialog" @getDeviceData="getDeviceData"/>
    <DetailDialog ref="detailDialog"/>
  </el-card>
</template>

<style scoped lang="scss">
.box-card {
  padding: 0 50px;

  .card-equip-title {
    height: 35px;
    display: flex;
    justify-content: space-between;
  }

  .card-equip-table {
    margin-top: 20px;

    .el-table-box {
      height: calc(90vh - 140px);

      .el-table__cell {
        position: static !important; // 解决el-image 和 el-table冲突层级冲突问题
      }
    }

    .equip-desc {
      display: -webkit-box; /*弹性伸缩盒子*/
      -webkit-box-orient: vertical; /*垂直排列*/
      line-height: 20px;
      -webkit-line-clamp: 1;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}

.dialog-item {
  display: flex;
  flex-direction: row;
  height: 30px;
  align-items: center;

  p {
    margin-left: 12px;
  }
}

.demo-ruleForm {
  padding: 0 60px;
}
</style>
